<?php

class AdminmenuController extends MyController
{
	function init() { 
		parent::init(); 
		$this->data["controller_id"] = $this->controller_id;
	}
	
	function actionIndex()
	{	
		$this->redirect(array($this->controller_id.'/view'));
	}	
	
	function actionView($parent_id = 0)
	{
		$rows = adminmenu::model()->findAll(array(
			"order" => "`order`"
		));
		
		$this->data['rows']= $rows;
		
		$this->render('view', $this->data);
	}
	
	function actionAdd()
	{
		$this->action = "Thêm mới Adminmenu";
		$row = new adminmenu;
		$row->id = 0;
		
		$this->data["row"] = $row;
		
		$this->render("form_edit", $this->data);
	}
	
	function actionEdit($adminmenu_id = 0)
	{
		if ($adminmenu_id == 0)
		{
			$this->redirect(array($this->data['controller_id'].'/view'));
			
		}else{
			$row = adminmenu::model()->findByPk($adminmenu_id);
			$this->data["row"] = $row;
			
			$this->action = "Cập nhật [" . $row->name ."]";
			$this->render("form_edit", $this->data);
		}
	}
	
	function actionSave($adminmenu_id = 0)
	{
		if ($adminmenu_id == 0)
		{
			$row = new adminmenu;
			$row->setAttributes($_POST, false);
			$row->enable = 1;
			if (strlen($row->name) > 0)
				$row->save();
		}else{
			$row = adminmenu::model()->findByPk($adminmenu_id);
			$row->setAttributes($_POST, false);
			if (strlen($row->name) > 0)
				$row->save();
		}
	
		$this->redirect(array($this->controller_id.'/view'));
	}
	
	function actionApply($adminmenu_id = 0)
	{
		if ($adminmenu_id == 0)
		{
			$row = new adminmenu;
			$row->setAttributes($_POST, false);
			$row->enable = 1;
			if (strlen($row->name) > 0)
				$row->save();
		}else{
			$row = adminmenu::model()->findByPk($adminmenu_id);
			$row->setAttributes($_POST, false);
			if (strlen($row->name) > 0)
				$row->save();
		}
	
		$this->redirect(array(
			$this->controller_id.'/edit', 
			"adminmenu_id" => $row->id
		));
	}
	
	function actionSavenew($adminmenu_id = 0)
	{
		if ($adminmenu_id == 0)
		{
			$row = new adminmenu;
			$row->setAttributes($_POST, false);
			$row->enable = 1;
			if (strlen($row->name) > 0)
				$row->save();
		}else{
			$row = adminmenu::model()->findByPk($adminmenu_id);
			$row->setAttributes($_POST, false);
			if (strlen($row->name) > 0)
				$row->save();
		}
	
		$this->redirect(array(
			$this->controller_id.'/add'
		));
	}
	
	function actionEnable($adminmenu_id = 0)
	{
		$row = adminmenu::model()->findByPk($adminmenu_id);
		$row->enable = 1 - $row->enable;
		$row->save();
		$this->redirect(array($this->data['controller_id'].'/view'));
	}
	
	function actionDefault($adminmenu_id = 0)
	{
		adminmenu::model()->updateAll(
			array('default'=>0)
		);
		$row = adminmenu::model()->findByPk($adminmenu_id);
		$row->default = 1;
		$row->save();
		$this->redirect(array($this->data['controller_id'].'/view'));
	}
	
	function actionConfirm_delete($adminmenu_id)
	{
		$row = adminmenu::model()->findByPk($adminmenu_id);
		
		$this->data['row'] = $row;
		
		$children = $row->sub_adminmenu;
		
		if (count($children) > 0)
			$this->render("message_delete", $this->data);
		else
			$this->render("confirm_delete", $this->data);
	}
	
	function actionDelete($adminmenu_id)
	{
		adminmenu::model()->deleteByPk($adminmenu_id);
		$this->redirect(array($this->data['controller_id'].'/view'));
	}
	
	function actionSet_order()
	{
		//Tao thu tu mac dinh
		$rows = adminmenu::model()->findAll();
		foreach($rows as $row)
		{
			$row->order = $row->id;
			$row->save();
		}
		
		$this->redirect(array($this->data['controller_id'].'/view'));	
	}
	
	function actionSave_order()
	{
		$orders = Yii::app()->getRequest()->getPost("order");
		
		foreach($orders as $id=>$value)
		{
			$adminmenu = adminmenu::model()->findByPk($id);
			$adminmenu->order = $value;
			$adminmenu->save();
		}
		
		//print_r($orders);
		$this->redirect(array($this->data['controller_id'].'/view'));		
	}
	
	function actionUp($adminmenu_id = 0)
	{
		$row = adminmenu::model()->findByPk($adminmenu_id);
		
		if($row->order > 1)
			$row->order--;
		
		$row->save();
		$this->redirect(array($this->data['controller_id'].'/view'));
	}
	
	function actionDown($adminmenu_id = 0)
	{
		$row = adminmenu::model()->findByPk($adminmenu_id);
		
		$row->order++;
		
		$row->save();
		$this->redirect(array($this->data['controller_id'].'/view'));
	}
	
	
}